<?php
/*
---------------------------------------------------------------------------
status/trigger.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require("../../library/frontend.php");

if (isset($_GET['id'])) {
	$host_trigger_id = check_xss($_GET['id']);
		$result = run_query("SELECT hosts_triggers.host_id, hosts_triggers.uptime, hosts_triggers.warningtime, hosts_triggers.criticaltime, hosts_triggers.last_check, hosts_triggers.current_state, hosts_triggers.current_info, hosts_triggers.current_data, hosts.name, triggers.id, triggers.name, checks.data_names, checks.data_types FROM hosts_triggers, hosts, triggers, checks WHERE hosts_triggers.id = '$host_trigger_id' AND hosts_triggers.host_id = hosts.id AND hosts_triggers.trigger_id = triggers.id AND triggers.check_id = checks.id LIMIT 1");
	if (mysql_num_rows($result) != NULL) {
		list($host_id, $uptime, $warning, $critical, $last_check, $state, $info, $data, $host_name, $trigger_id, $trigger_name, $data_names, $data_types) = mysql_fetch_array($result);
		function jscripthead() {
			global $host_id, $trigger_id, $data_names, $data_types, $data;
			echo '<script src="graphdata.php?hostid=', $host_id, '&triggerid=', $trigger_id, '&datanames=', $data_names, '&datatypes=', $data_types, '" type="text/javascript"></script>
			<script type="text/javascript">

				var largeview;

				function onChronoscopeLoaded(chrono)
				{
				chronoscope.Chronoscope.setFontBookRendering(false);
				chronoscope.Chronoscope.setErrorReporting(true);
					var graphdata = [';
					$columns = explode("|", $data);
					$firstrow = TRUE;
					foreach ($columns as $key => $value) {
						if ($firstrow == TRUE) {
							echo 'triggerdata', $key;
							$firstrow = FALSE;
						}
						else
							echo ', triggerdata', $key;
					}
					echo '];
					chronoscope.Chronoscope.createTimeseriesChartById("graph", graphdata, 650, 433, function(view)
					{
						var plot = view.getChart().getPlot();
						plot.getAxis(0).setAutoZoomVisibleRange(true);
						plot.reloadStyles();
						view.getChart().getPlot().redraw();
					});
				 }  

			</script>
			<script src="http://api.timepedia.org/widget/chronoscope.js"></script>';
		}
		page_start($trigger_name . ' on ' . $host_name);
		echo '
		<table>
		<tr><th>Current State:</th><td class="', return_state($state), '">', return_state($state), '</td></tr>
		<tr><th>Last Check:</th><td>', date("D M j G:i:s T Y", $last_check), '</td></tr>
		<tr><th>Current Info:</th><td>', $info, '</td></tr>
		<tr><th>Current Data:</th><td>', $data, '</td></tr>
		<tr><th>Uptime:</th><td>', convert_seconds($uptime), ' | ', number_format(@($uptime/($uptime + $warning + $critical) * 100), 2), '%</td></tr>
		<tr><th>Warning:</th><td>', convert_seconds($warning), ' | ', number_format(@($warning/($uptime + $warning + $critical) * 100), 2), '%</td></tr>
		<tr><th>Critical:</th><td>', convert_seconds($critical), ' | ', number_format(@($critical/($uptime + $warning + $critical) * 100), 2), '%</td></tr>
		</table>';
		echo '
		<h3>Graph</h3>
		<div id="graph" class="chronoscope" style="width:650px;height:433px;">Loading Graph...</div>';
		$data_result = run_query("SELECT time, state, data FROM trigger_data WHERE host_id = '$host_id' AND trigger_id = '$trigger_id' ORDER BY time DESC LIMIT 10");
		if (mysql_num_rows($data_result) != NULL) {
			echo '
			<h3>Previous 10 Results</h3>
			<table>
			<tr><th>Time:</th><th>State:</th><th>Data:</th></tr>';
			while(list($time, $state, $data) = mysql_fetch_array($data_result)) {
				echo '<tr><td>', date("D M j G:i:s T Y", $time), '</td><td class="', return_state($state), '">', return_state($state), '</td><td>', $data, '</td></tr>';
			}
			echo '</table>';
		}
	}
	echo '<br /><a href="host.php?id=' , $host_id , '">Back to ', $host_name, '</a>';
}
page_end();
?>